package com.liangtian.mdm.dm.tree.service.impl;

import com.alibaba.fastjson.JSON;
import com.liangtian.mdm.dm.config.model.AttrConfig;
import com.liangtian.mdm.dm.listener.CacheUtil;
import com.liangtian.mdm.dm.tree.dao.TreeDao;
import com.liangtian.mdm.dm.tree.model.Tree;
import com.liangtian.mdm.dm.tree.service.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TreeServiceImpl implements TreeService {

    @Autowired
    private TreeDao treeDao;

//	@Autowired
//	private AttrConfigDao attrConfigDao;

    @Autowired
    private CacheUtil cacheUtil;

    @Override
    public List<Tree> queryChildren(Map<String, Object> param) {
        List<Tree> treeList = null;
        try {
            String tableName = param.get("tableName").toString();
            String column = param.get("column").toString();
            String value = param.get(column).toString();
            String modelId = tableName.substring(tableName.indexOf("_") + 1);
            //树显示节点
            StringBuilder comb = new StringBuilder();
            List<AttrConfig> acList = cacheUtil.getAttrList(modelId);
            for (AttrConfig ac : acList) {
                if ("1".equals(String.valueOf(ac.getIsTreeNode()))) {
                    if ("".equals(comb.toString())) {
                        comb.append(ac.getAttrColumn());
                    } else {
                        comb.append("||'_'||").append(ac.getAttrColumn());
                    }
                }
            }
            if ("".equals(comb.toString())) {
                comb.append("id");
            }
            //根据属性设置的排序，给左侧树节点排序
            StringBuilder sort = new StringBuilder();
            for (AttrConfig ac : acList) {
                if (ac.getIsOrder() == 1) {
                    sort.append(", ").append(ac.getAttrColumn());
                    if (ac.getOrderRule() == 1) {
                        sort.append(" asc");
                    } else if (ac.getOrderRule() == 2) {
                        sort.append(" desc");
                    }
                }
            }
            sort.append(", id desc ").append(", ").append("version").append("  asc");
            param.put("orderBy", sort.substring(1));

            param.put("combineCols", comb.toString());
            param.put("column", column);
            param.put("value", value);
            System.out.println(JSON.toJSONString(param));
            treeList = treeDao.queryChildren(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return treeList;
    }

    /**
     * * <p>Title:  弹出树用到的数据来源</p>
     * <p>Description: TODO</p>
     *
     * @param : @param param
     * @param : @return
     * @author : xiaoliu
     * @date :2016年10月25日下午5:47:35
     */

    @Override
    public List<Tree> queryTreeChildren(Map<String, Object> param) {
        List<Tree> treeList = null;
        try {
            //String tableName = param.get("tableName").toString();
            //String modelId = tableName.substring(tableName.indexOf("_")+1);//获取数据来源的modelId
            String modelIdTree = param.get("modelIdTree").toString();//原模型的modelId
            String attrColumnTree = param.get("attrColumnTree").toString();//原模型的列名


            StringBuilder comb = new StringBuilder();
            //获取原模型的这条数据的属性配置信息
            AttrConfig acList = cacheUtil.getAttrConfig(modelIdTree, attrColumnTree);
            String arr[] = acList.getDataSourceColumn().split(",");
            for (int i = 0; i < arr.length; i++) {
                String colum = arr[i];
                if ("".equals(comb.toString())) {
                    comb.append(colum);
                } else {
                    comb.append("||'_'||").append(colum);
                }
            }
            if ("".equals(comb.toString())) {
                comb.append("id");
            }

            //根据属性设置的排序，给左侧树节点排序
            StringBuilder sort = new StringBuilder();
            List<AttrConfig> mList = cacheUtil.getAttrList(modelIdTree);
            for (AttrConfig ac : mList) {
                if (ac.getIsOrder() == 1) {
                    sort.append(", ").append(ac.getAttrColumn());
                    if (ac.getOrderRule() == 1) {
                        sort.append(" asc");
                    } else if (ac.getOrderRule() == 2) {
                        sort.append(" desc");
                    }
                }
            }
            sort.append(", id desc ").append(", ").append("version").append("  asc");
            param.put("orderBy", sort.substring(1));

            param.put("combineCols", comb.toString());
            String column = attrColumnTree.toString();
            param.put("column", column);
            String value = param.get(column).toString();
            param.put("value", value);
            //System.out.println(JSON.toJSONString(param));
            treeList = treeDao.queryChildren(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return treeList;
    }


    @Override
    public Map<String, Object> queryOne(Map<String, Object> param) {

        return treeDao.selectByPk(param);
    }

    /***
     * @param : @param map
     * @param : @return
     * @author : xiaoliu
     * @date :2016年12月13日下午1:35:45
     */

    @Override
    public List<Tree> getChildrenById(Map<String, Object> param) {
        List<Tree> treeList = null;
        try {
            String tableName = param.get("tableName").toString();
            String column = param.get("attrColumn").toString();
            String value = param.get(column.toLowerCase()).toString();
            String modelId = tableName.substring(tableName.indexOf("_") + 1);
            //树显示节点
            StringBuilder comb = new StringBuilder();
            List<AttrConfig> acList = cacheUtil.getAttrList(modelId);
            for (AttrConfig ac : acList) {
                if ("1".equals(String.valueOf(ac.getIsTreeNode()))) {
                    if ("".equals(comb.toString())) {
                        comb.append(ac.getAttrColumn());
                    } else {
                        comb.append("||'_'||").append(ac.getAttrColumn());
                    }
                }
            }
            if ("".equals(comb.toString())) {
                comb.append("id");
            }
            param.put("combineCols", comb.toString());
            param.put("column", column);
            param.put("value", value);
            //System.out.println(JSON.toJSONString(param));
            treeList = treeDao.getChildrenById(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return treeList;
    }

}
